function [pR RT]= modelRT2D( d, c)

num_c = length(c);
nT = size(d,1);
tR = zeros(num_c,1);
tL = tR;
pR = tR;
isSampling = zeros(nT,nT + 2);
isRight = zeros(nT,nT + 2);
isLeft = zeros(nT,nT + 2);

for t  = 1:nT
    for i = 1 : t
        if d(t,i) == 0
            isSampling(t,i) = 1;
        elseif mod(d(t,i),2) == 0
            isLeft(t,i) = 1;
        else
            isRight(t,i) = 1;
        end
    end
end

for i = 1 : num_c
    u = (c(i) + 1)/2;
    filterPDF = zeros(nT, nT + 1); %P(u_{t}, u_{1:t-1} \in sampling)
    condP_R = zeros(1,nT); %P(u_{t+1} \in right, u_{1:t} \in sampling)
    condP_L =  condP_R;  %P(u_{t+1} \in left, u_{1:t} \in sampling)
    filterPDF(1,1) = 1 - u;
    filterPDF(1,2) = u;
    for t = 1 : nT - 1
        filterPDF(t,nT+2) = 0;
        lastSampled = find(isSampling(t,:));
        filterPDF(t+1,lastSampled) = filterPDF(t+1,lastSampled) + ...
            filterPDF(t,lastSampled)* (1-u) .* isSampling(t+1,lastSampled);
        filterPDF(t+1,lastSampled + 1) = filterPDF(t+1,lastSampled + 1) +...
            filterPDF(t,lastSampled) * u .* isSampling(t+1,lastSampled + 1);
        condP_R(t+1) = sum(filterPDF(t, lastSampled ) * (1-u) .* isRight(t+1,lastSampled) + ...
            filterPDF(t, lastSampled)* u .* isRight(t+1,lastSampled+1));
        condP_L(t+1) = sum(filterPDF(t, lastSampled )* (1-u) .* isLeft(t+1,lastSampled) + ...
            filterPDF(t,lastSampled )* u .* isLeft(t+1,lastSampled +1));
    end
    sumR = sum(condP_R);
    sumL = sum(condP_L);
    pR(i) = sumR / (sumR + sumL);
    condP_R = condP_R / sumR;
    condP_L = condP_L / sumL;
    tR(i) = condP_R * (1:nT)' ;
    tL(i) = condP_L * (1:nT)';
end
RT = [tR tL];
%plot(condP_R(1:2:end));
end

